package com.kehao.leetcode.middle.math;

import org.junit.Test;

import java.util.HashSet;

public class HappyNumberSolution {

    public boolean isHappy(int n) {
        if ( n == 1 ) return  true;
        HashSet<Integer> set = new HashSet<>();
        set.add(n);
        while(n!=1){
            n = getNext(n);
            if(set.contains(n)){
                return false;
            }else {
                set.add(n);
            }
        }
        return true;
    }

    private int getNext(int n) {
        int result =0;
        int a = 0;
        while (n!=0){
            a = n%10;
            n/=10;
            result+=a*a;
        }
        return result;
    }

    @Test
    public void test01(){
        System.out.println(isHappy(19));
        System.out.println(isHappy(2));
    }
}
